* build_var_employment	JEP	05/01/15
* Builds employment and unemployment variables

*************************** Change log ********************************** 

/*
	
*/	

clear 
version 13.1

* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" loc master "B:\Col_Minimum_Wage"
	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
 loc pr = 0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'"  
 loc pr = 1
	* Project calls
	project, uses("`master'/Data/Source/fuerza_trabajo.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/sample_choice_desocupados.do")	
}
*/

di 84
* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/sample_choice_desocupados.do"



* foreach x in formal informal_idom informal_edom {
foreach x in informal {
	
	clear
	use "`master'/Data/Source/fuerza_trabajo.dta", clear
	
	
	
	* Muy poquita gente en el Valle de Aburrá
	drop if area==6
	* Resultado final
	keep if inlist(area,5,6,8,11,17,52,68,76)
	
	
	if "`x'"=="formal" loc y="`x'"
	else if "`x'"=="informal_idom" loc y="informal domestic"
	else loc y="informal"
	
	
	
	* Choose sample - ocupados
	mysample e4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) `y' exind(13,29,42)
	
	
	


	* mysample e4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
	replace e4sample = (e4sample ==1 & ocupado==3)
	* Choose sample - desocupados
	mysample_desocupados d4sample , sex(0) edadm(65) exind(13,29,42)
	replace d4sample = (d4sample ==1 & desocupado==3)
	* Choose sample - inactivos. Can use desocupados without exind for this one
	mysample_desocupados i4sample , sex(0) edadm(65) 
	replace i4sample = (i4sample ==1 & inactivo==3)
	
	tab e4sample time
	tab i4sample time
	tab d4sample time
	
	
	
	
	
	keep if e4sample | i4sample | d4sample
	
	

	* Generate one variable
	gen catemp=.
	replace catemp=1 if e4sample
	replace catemp=2 if d4sample
	replace catemp=3 if i4sample
	
	

	gen tocount=1

	* Put all inactivos in 1 industry to be able to collapse
	replace cactividad_empresa=999 if i4sample
	replace cactividad_empresa=cbuscado_actividad if d4sample
	replace cactividad_empresa=ctrabajoo_actividad if d4sample & cactividad_empresa==.
	* Count by city industry cell
	decode area, gen(ciudad)
	encode ciudad, gen(ccode)

	collapse (count) num=tocount [iw=fex], by(ccode cactividad_empresa catemp time)
	
	drop if cactividad==.
	
	label define catemp 1 "Ocupado" 2 "Desocupado" 3 "Inactivo"
	la val catemp catemp
	codebook ccode if catemp!=. & num!=.
	codebook cactividad if catemp!=. & num!=.
	codebook time if catemp!=. & num!=.
	codebook time if catemp!=. & num!=.
	
	
	
	reshape wide num, i(ccode cactividad_empresa time) j(catemp)
	

	ren num1 o_ind
	ren num2 d_ind
	ren num3 i_ind
	codebook time if o_ind!=.
	* Employment by city-industry
	gen e_od_ind_ind=o_ind/(o_ind+d_ind)
	* Employment by industry at the city level 
	bys ccode time: egen o_c = sum(o_ind)
	bys ccode time: egen d_c = sum(d_ind)
	bys ccode time: egen i_c = sum(i_ind)
	gen e_od_ind_c= o_ind/(o_c + d_c)
	gen e_odi_ind_c = o_ind / (o_c + d_c + i_c)
	* Employment at the city level
	gen e_od_c_c = o_c / (o_c + d_c)
	gen e_odi_c_c = o_c / (o_c + d_c + i_c)

	* Unemployment by industry
	gen d_ind_ind= d_ind / (o_ind + d_ind)
	gen d_ind_c = d_ind / (o_c + d_c)
	gen d_c_c = d_c / (o_c + d_c)
	
	bys cactividad_empresa time: egen empind=sum(o_ind)
	
	save "`master'/Data/Source/empvars_`x'.dta", replace
	
}

if `pr' project, creates("`master'/Data/Source/empvars_formal.dta")
if `pr' project, creates("`master'/Data/Source/empvars_informal_idom.dta")
if `pr' project, creates("`master'/Data/Source/empvars_informal_edom.dta")






